﻿/**
 * Create in 2012-4-3.
 */
package kit4j.tests.util;

import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * 
 * @author MiniKnife
 *
 */
public abstract class AWTFactory {

	public static JFrame createJFrame(String title, int width, int height) {
		JFrame frame = new JFrame(title);
		
		setWindowSizeAndLocation(frame, width, height);
		setDefaultWindowCloseOperation(frame);
		
		return frame;
	}
	
	private static void setWindowSizeAndLocation(Window window, int width, int height) {
		window.setSize(width, height);
		window.setLocationRelativeTo(null);
	}
	
	private static void setDefaultWindowCloseOperation(JFrame frame) {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private static void setDefaultWindowCloseOperation(final Window window) {
		window.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent evt) {
				window.setVisible(false);
				window.dispose();
				
				System.exit(0);
			}
		});
	}
	
	public static Window createWindow(Window owner, int width, int height) {
		Window window = new Window(owner);
		
		setWindowSizeAndLocation(window, width, height);
		setDefaultWindowCloseOperation(window);
		
		return window;
	}
	
	public static JButton createJButton(String label) {
		return new JButton(label);
	}
	
	public static JButton createJButton() {
		return new JButton();
	}
}
